package com.sls.nzdapplication.ui.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bin.david.form.core.SmartTable;
import com.bin.david.form.data.column.Column;
import com.bin.david.form.data.style.FontStyle;
import com.bin.david.form.data.table.TableData;
import com.esri.arcgisruntime.geometry.GeodeticCurveType;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.LinearUnit;
import com.esri.arcgisruntime.geometry.LinearUnitId;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.PointCollection;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.ess.filepicker.FilePicker;
import com.ess.filepicker.model.EssFile;
import com.ess.filepicker.util.Const;
import com.hmy.popwindow.PopItemAction;
import com.hmy.popwindow.PopWindow;
import com.markfrain.formview.view.FormEditView;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.app.AppActivity;
import com.sls.nzdapplication.app.MySharedPreferences;
import com.sls.nzdapplication.db.utils.DyEquipmentLineUtil;
import com.sls.nzdapplication.db.utils.DyEquipmentUtil;
import com.sls.nzdapplication.db.utils.MeterBoxUtil;
import com.sls.nzdapplication.db.utils.MeterUtil;
import com.sls.nzdapplication.db.utils.StationAreaUtil;
import com.sls.nzdapplication.db.utils.SwitchEquipmentUtil;
import com.sls.nzdapplication.db.utils.SwitchUtil;
import com.sls.nzdapplication.db.utils.UserDataUtil;
import com.sls.nzdapplication.model.DyEquipment;
import com.sls.nzdapplication.model.DyEquipmentLine;
import com.sls.nzdapplication.model.DyWorkInfo;
import com.sls.nzdapplication.model.ExportEquipment;
import com.sls.nzdapplication.model.Meter;
import com.sls.nzdapplication.model.MeterBox;
import com.sls.nzdapplication.model.StationArea;
import com.sls.nzdapplication.model.Switch;
import com.sls.nzdapplication.model.SwitchEquipment;
import com.sls.nzdapplication.model.TableViewData;
import com.sls.nzdapplication.model.User;
import com.sls.nzdapplication.myapp.ZDAPP;
import com.sls.nzdapplication.ui.custom.CableWellPhotoView;
import com.sls.nzdapplication.ui.popup.ListPopup;
import com.sls.nzdapplication.util.DeviceUtils;
import com.sls.nzdapplication.util.ExcelUtils;
import com.sls.nzdapplication.util.LatlonUtil;
import com.sls.nzdapplication.util.SnowFlakeUtil;
import com.sls.nzdapplication.util.Utils;
import com.sls.nzdapplication.util.UuidUtils;
import com.sls.nzdapplication.util.ZipClass;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import es.dmoral.toasty.Toasty;
import jxl.Sheet;
import jxl.Workbook;

/**
 * 这个类是 DyWorkActivity，是一个继承自 AppCompatActivity 的 Activity 类。这个类主要是用于低压压工作的 Activity，主要包括以下功能：
 *
 * 初始化视图
 * 初始化数据
 * 显示数据
 * 新建数据
 * 打开工作
 * 删除数据
 * 选择新区域
 * */
public class DyWorkActivity extends AppActivity implements View.OnClickListener, CableWellPhotoView.OnclickPhotoListener{
    private LinearLayout workDataView;

    private StationAreaUtil stationAreaUtil;

    private EditText workNameView;

    private TextView startWork;

    private RadioGroup radioGroup;

    private MySharedPreferences mySharedPreferences;

    private String type = "站内";


    private static final int REQUEST_CODE_CHOOSE = 23;

    //导入类型 0用户数据 1历史数据包
    private int updateType = 0;

    private UserDataUtil userDataUtil;

    PopWindow  popupWindow;

    PopWindow  popupWindow2;

    PopWindow  popupWindow3;

    ImageView areaListView;

    CableWellPhotoView mpPhotoView,qmPhotoView;

    private String photoType;
    DyEquipmentUtil dyEquipmentUtil;
    DyEquipmentLineUtil dyEquipmentLineUtil;
    MeterBoxUtil meterBoxUtil;
    MeterUtil meterUtil;
    SwitchEquipmentUtil switchEquipmentUtil;
    SwitchUtil switchUtil;

    List<DyWorkInfo> dyWorkInfos;

    List<StationArea> workLines;

    public static final String SD_APP_DIR_NAME = "NZDAPP";

    private String autopath = Environment.getExternalStorageDirectory().getAbsolutePath()
            + "/" + SD_APP_DIR_NAME + "/";


    private String zippath = autopath + "数据备份/";

    private boolean updatePhoto = false;
    SmartTable<DyWorkInfo> table;

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dy_work;
    }

    @Override
    protected void initView() {
        workNameView = findViewById(R.id.tq_name);
        startWork = findViewById(R.id.start_dyyp);
        radioGroup = findViewById(R.id.area_type);
        areaListView = findViewById(R.id.area_list);
        areaListView.setOnClickListener(this);
        stationAreaUtil = new StationAreaUtil(getApplicationContext());
        dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        dyEquipmentLineUtil = new DyEquipmentLineUtil(getApplicationContext());
        meterBoxUtil = new MeterBoxUtil(getApplicationContext());
        meterUtil = new MeterUtil(getApplicationContext());
        switchEquipmentUtil = new SwitchEquipmentUtil(getApplicationContext());
        switchUtil = new SwitchUtil(getApplicationContext());
        userDataUtil = new UserDataUtil(getApplicationContext());
        table = (SmartTable<DyWorkInfo>) findViewById(R.id.dywork_data);
        table.getConfig().setShowXSequence(false);
        table.getConfig().setShowYSequence(false);
        table.getConfig().setShowTableTitle(false);
        table.getConfig().setColumnTitleStyle (new FontStyle(30, getResources().getColor(R.color.colorPrimaryDark)));


        mySharedPreferences = new MySharedPreferences(getApplicationContext());
        startWork.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());
                if(rb_temp.getText().toString().equals("站内变")){
                    type = "站内";
                }else{
                    type = "台架";
                }
            }
        });
    }

    @Override
    protected void initData() {
        workLines = stationAreaUtil.queryStationAreaByStatue("开始采集");
        new InitdateTask().execute();
    }

    private void showData(List<StationArea> titleData) {
        table.setTableData(null);
        dyWorkInfos = new ArrayList<>();
        for (int i = 0; i < titleData.size(); i++) {
            StationArea stationArea = titleData.get(i);
            DyWorkInfo dyWorkInfo = new DyWorkInfo();
            dyWorkInfo.setNumber(String.valueOf(i+1));
            dyWorkInfo.setName(stationArea.getName());
            dyWorkInfo.setType(stationArea.getType());
            dyWorkInfo.setBx(String.valueOf(meterBoxUtil.queryMeterBoxByLineId(stationArea.getId()).size()));
            List<MeterBox> meterBoxList = meterBoxUtil.queryMeterBoxByLineId(stationArea.getId());
            int meterNumber = 0;
            for(MeterBox meterBox:meterBoxList){
                meterNumber+= meterUtil.queryMetersByBoxId(meterBox.getId()).size();
            }
            dyWorkInfo.setUser(String.valueOf(meterNumber));
            dyWorkInfo.setSystem(String.valueOf(userDataUtil.queryUserByAreaId(stationArea.getId()).size()));
            dyWorkInfo.setDate(stationArea.getCreateTime());
            dyWorkInfos.add(dyWorkInfo);
        }


    }

    private void newData(){
        Calendar calendar = Calendar.getInstance ();
        String name = workNameView.getText().toString();
        if(name.equals("")){
            Toasty.error(getApplicationContext(), "台区名称不能为空", Toast.LENGTH_LONG, true).show();
            return;
        }
        if(stationAreaUtil.queryStationAreaByAreaName(name)!=null){
            Toasty.error(getApplicationContext(), "台区名称不能重复", Toast.LENGTH_LONG, true).show();
            return;
        }
        StationArea stationArea = new StationArea();
        stationArea.setId(UuidUtils.getStringID());
        stationArea.setName(name);
        stationArea.setType(type);
        stationArea.setUpdateType("diy");
        stationArea.setStatue("开始采集");
        stationArea.setCreateTime(calendar.get(calendar.YEAR)+"年"+(calendar.get(calendar.MONTH)+1)+"月"+calendar.get(calendar.DATE)+"日");
        stationArea.setCreateBy(mySharedPreferences.getUserValue("username"));
        stationAreaUtil.insertStationArea(stationArea);
        Toasty.success(getApplicationContext(), "保存成功", Toast.LENGTH_LONG, true).show();
        openWork(stationArea);
    }

    private void openWork(StationArea stationArea){
        stationArea.setStatue("start");
        stationAreaUtil.updateStationArea(stationArea);
        Intent intent = new Intent(DyWorkActivity.this,DyMapActivity.class);
        intent.putExtra("lineId",stationArea.getId());
        startActivity(intent);
    }


    private void delCom(StationArea stationArea){
        new PopWindow.Builder(DyWorkActivity.this)
                .setStyle(PopWindow.PopWindowStyle.PopUp)
                .setTitle("你确定要删除当前数据！！")
                .addItemAction(new PopItemAction("确定", PopItemAction.PopItemStyle.Warning, new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        deleteData(stationArea);
                    }
                }))
                .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Cancel))
                .show();
    }

    private void deleteData(StationArea stationArea){

//        UserDataUtil userDataUtil = new UserDataUtil(getApplicationContext());
//        List<User> users = userDataUtil.queryUserByAreaId(stationArea.getId());
//        for(User u:users){
//            userDataUtil.deleteUser(u);
//        }
        List<DyEquipment> dyEquipmentList = dyEquipmentUtil.queryDyEquipmentByAreaId(stationArea.getId());
        for(DyEquipment dyEquipment:dyEquipmentList){
            List<Switch> switchList = switchUtil.querySwitchByeId(dyEquipment.getId());
            for(Switch sw:switchList){
                List<SwitchEquipment> switchEquipmentList = switchEquipmentUtil.querySwitchEquipmentBySid(sw.getId());
                for(SwitchEquipment switchEquipment :switchEquipmentList){
                    switchEquipmentUtil.deleteSwitchEquipment(switchEquipment);
                }
                switchUtil.deleteSwitch(sw);
            }
            dyEquipmentUtil.deleteDyEquipment(dyEquipment);
        }

        List<DyEquipmentLine> dyEquipmentLineList = dyEquipmentLineUtil.queryEquipmentLineByLineId(stationArea.getId());
        for(DyEquipmentLine dyEquipmentLine:dyEquipmentLineList){
            dyEquipmentLineUtil.deleteDyEquipmentLine(dyEquipmentLine);
        }

        List<MeterBox> meterBoxList = meterBoxUtil.queryMeterBoxByLineId(stationArea.getId());
        for(MeterBox meterBox:meterBoxList){
            List<Meter> meterList = meterUtil.queryMetersByBoxId(meterBox.getId());
            for(Meter meter:meterList){
                meterUtil.deleteMeter(meter);
            }
            meterBoxUtil.deleteMeterBox(meterBox);
        }
        if(stationArea.getUpdateType().equals("diy")){
            stationAreaUtil.deleteStationArea(stationArea);
        }else{
            stationArea.setStatue("结束采集");
            stationAreaUtil.updateStationArea(stationArea);
        }
        initData();
    }

    @Override
    public void onClick(View view) {
        if(view == startWork){
            newData();
        }
        if(view == areaListView){
            selectNewArea();
        }
    }

    private void selectNewArea(){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,600);
        List<String> areaName = new ArrayList<>();
        ListView listView = new ListView(getApplicationContext());
        listView.setLayoutParams(layoutParams);
        List<StationArea> workLines = stationAreaUtil.queryStationAreaByUpdateType("update");

        for(StationArea stationArea:workLines){
            areaName.add(stationArea.getName());
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                StationArea stationArea = workLines.get(i);
                if(stationArea.getStatue().equals("开始采集")){
                    Toasty.info(getApplicationContext(), "该台区已经开始采集", Toast.LENGTH_LONG, true).show();
                }else{
                    workLines.get(i).setStatue("开始采集");
                    stationAreaUtil.updateStationArea(workLines.get(i));
                    initData();
                    popupWindow2.dismiss();

                }

            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getApplicationContext(), android.R.layout.simple_list_item_1, areaName);
        listView.setAdapter(adapter);
        popupWindow2= new PopWindow.Builder(DyWorkActivity.this)
                .setStyle(PopWindow.PopWindowStyle.PopAlert)
                .setMessage("请选择台区开始采集")
                .addContentView(listView)
                .setIsShowCircleBackground(false)
                .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Warning))
                .setPopWindowMarginsF(DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),200), DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),0))
                .show();
    }

    @Override
    public void onRightClick(View view) {
        new PopWindow.Builder(this)
                .setStyle(PopWindow.PopWindowStyle.PopAlert)
                .setMessage("数据设置")
                .setIsShowCircleBackground(false)
                .addItemAction(new PopItemAction("导入用户数据", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        updateType = 0;
                        openFile();
                    }
                }))
                .addItemAction(new PopItemAction("导出单个台区数据", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        selectArea();
                    }
                }))
                .addItemAction(new PopItemAction("导出所有台区数据", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        updateAll();
                    }
                }))
                .addItemAction(new PopItemAction("数据库备份", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        updateHistory();
                        Toasty.success(getApplicationContext(),   "备份成功", Toast.LENGTH_LONG, true).show();
                    }
                }))
                .addItemAction(new PopItemAction("数据库备份(含照片)", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        updatePhoto = true;
                        updateHistory();
                        Toasty.success(getApplicationContext(),   "备份成功", Toast.LENGTH_LONG, true).show();
                    }
                }))
                .addItemAction(new PopItemAction("导入历史数据包", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        updateType = 1;
                        puthistory();
                    }
                }))
                .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Warning))
                .setPopWindowMarginsF(DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),300), DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),0))
                .show();
    }

    private void updateAll(){
        List<StationArea> workLines = stationAreaUtil.queryStationAreaByStatue("开始采集");
        for(StationArea stationArea:workLines){
            updateAreaDate(stationArea);
        }
    }




    private void updateHistory(){
        try{
            if(stationAreaUtil.queryAll().size()>0){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("dyEquipment",dyEquipmentUtil.queryAllToJson());
                jsonObject.put("dyEquipmentLine",dyEquipmentLineUtil.queryAllToJson());
                jsonObject.put("meterBox",meterBoxUtil.queryAllToJson());
                jsonObject.put("meter",meterUtil.queryAllToJson());
                jsonObject.put("stationArea",stationAreaUtil.queryAllToJson());
                jsonObject.put("switch",switchUtil.queryAllToJson());
                jsonObject.put("switchEquipment",switchEquipmentUtil.queryAllToJson());
                jsonObject.put("user",userDataUtil.queryAllToJson());
                String date = SnowFlakeUtil.getStringDate();
                Utils.writeTxtToFile(jsonObject.toJSONString(),zippath+"低压/"+ date,"/dataBag.txt");
                if(updatePhoto){
                    updateHistoryPhoto(date);
                }
            }
            updatePhoto = false;
        }catch (Exception e){
            Toast.makeText(getApplicationContext(),"导出备份失败",Toast.LENGTH_LONG).show();
        }

    }

    //导出照片
    private void updateHistoryPhoto(String date){
        String nPath = zippath +"低压/"+ date+"/dyEquipment";
        String oPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/dyEquipment";
        Utils.copyFolder(oPath,nPath);


        String nPath1 = zippath +"低压/"+ date+"/MeterBox";
        String oPath1 = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/MeterBox";
        Utils.copyFolder(oPath1,nPath1);

        String zPath = zippath +"低压/"+ date;

        ZipClass.compressFile(zPath,  date+".zip");
    }

    private void clearData(){
        dyEquipmentUtil.deleteAll(DyEquipment.class);
        dyEquipmentLineUtil.deleteAll(DyEquipmentLine.class);
        meterBoxUtil.deleteAll(MeterBox.class);
        meterUtil.deleteAll(Meter.class);
        stationAreaUtil.deleteAll(StationArea.class);
        switchUtil.deleteAll(Switch.class);
        switchEquipmentUtil.deleteAll(SwitchEquipment.class);
        userDataUtil.deleteAll(User.class);
    }


    private void initHistory(EssFile file){
        if(file.getFile().getName().indexOf(".zip")>-1){
            try{
                String oPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                        + "/NZDAPP/"+file.getFile().getName();
                ZipClass.unZip(file.getFile(),oPath);
                String fPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                        + "/NZDAPP/"+file.getFile().getName()+"/"+file.getFile().getName()+"/"+"dataBag.txt";
                String date = getFileContent(new File(fPath));
                com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(date);
                dyEquipmentUtil.insertByJsArray(jsonObject.getJSONArray("dyEquipment"));
                dyEquipmentLineUtil.insertByJsArray(jsonObject.getJSONArray("dyEquipmentLine"));
                meterBoxUtil.insertByJsArray(jsonObject.getJSONArray("meterBox"));
                meterUtil.insertByJsArray(jsonObject.getJSONArray("meter"));
                stationAreaUtil.insertByJsArray(jsonObject.getJSONArray("stationArea"));
                switchUtil.insertByJsArray(jsonObject.getJSONArray("switch"));
                switchEquipmentUtil.insertByJsArray(jsonObject.getJSONArray("switchEquipment"));
                userDataUtil.insertByJsArray(jsonObject.getJSONArray("user"));
                initData();
                initPhoto(file.getFile().getName());
                Toasty.success(getApplicationContext(),   "初始化成功", Toast.LENGTH_LONG, true).show();
            }catch (Exception e){
                e.printStackTrace();
                Toasty.error(getApplicationContext(),   "导入失败", Toast.LENGTH_LONG, true).show();
            }

        }else{
            String date = getFileContent(file.getFile());
            System.err.println(date);
            JSONObject jsonObject = JSONObject.parseObject(date);
            dyEquipmentUtil.insertByJsArray(jsonObject.getJSONArray("dyEquipment"));
            dyEquipmentLineUtil.insertByJsArray(jsonObject.getJSONArray("dyEquipmentLine"));
            meterBoxUtil.insertByJsArray(jsonObject.getJSONArray("meterBox"));
            meterUtil.insertByJsArray(jsonObject.getJSONArray("meter"));
            stationAreaUtil.insertByJsArray(jsonObject.getJSONArray("stationArea"));
            switchUtil.insertByJsArray(jsonObject.getJSONArray("switch"));
            switchEquipmentUtil.insertByJsArray(jsonObject.getJSONArray("switchEquipment"));
            userDataUtil.insertByJsArray(jsonObject.getJSONArray("user"));
            initData();
            Toasty.success(getApplicationContext(),   "初始化成功", Toast.LENGTH_LONG, true).show();
        }

    }

    private void initPhoto(String filename){
        String nPath = autopath+ filename+"/"+filename+"/dyEquipment";
        String oPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/dyEquipment";
        Utils.copyFolder(nPath,oPath);

        String nPath1 = autopath+ filename+"/"+filename+"/MeterBox";
        String oPath1 = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/MeterBox";
        Utils.copyFolder(nPath1,oPath1);

        ZipClass.deleteDirWihtFile(new File(autopath+ filename));
        ZipClass.deleteDirWihtFile(new File(autopath+ filename+filename));
    }

    private void selectArea(){
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,600);
        List<String> areaName = new ArrayList<>();
        ListView listView = new ListView(getApplicationContext());
        listView.setLayoutParams(layoutParams);
        List<StationArea> workLines = stationAreaUtil.queryStationAreaByStatue("开始采集");

        for(StationArea stationArea:workLines){
            areaName.add(stationArea.getName());
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                updateAreaDate(workLines.get(i));
//                popupWindow.dismiss();
            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                getApplicationContext(), android.R.layout.simple_list_item_1, areaName);
        listView.setAdapter(adapter);
        popupWindow= new PopWindow.Builder(DyWorkActivity.this)
                .setStyle(PopWindow.PopWindowStyle.PopAlert)
                .setMessage("请选择台区")
                .addContentView(listView)
                .setIsShowCircleBackground(false)
                .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Warning))
                .setPopWindowMarginsF(DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),200), DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),0))
                .show();
    }

    private void updateAreaDate(StationArea stationArea){
        exportEquipmentList(stationArea);
        exportMeterList(stationArea);
        exportDymodel(stationArea);
        exportDyDX(stationArea);
        exportDyDLD(stationArea);
        exportDyGTQD(stationArea);
        exportJLXMB(stationArea);
        exportUser(stationArea);
        Toasty.success(getApplicationContext(),   "导出成功", Toast.LENGTH_LONG, true).show();
    }

    //低压模板
    private void exportDymodel(StationArea stationArea){
        try {
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"低压模板");
            exportEquipment.setFolderName(stationArea.getName());
            exportExcelModel(getApplicationContext(),exportEquipment,stationArea);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"导出低压模板失败",Toast.LENGTH_LONG).show();
        }

    }

    private void exportUser(StationArea stationArea){
        String[] titles = {"序号","电表局编号","用户编号","用户名称","用电地址","表箱号","修改后表箱号","采集序号"};
        List<List<String>> dataList = new ArrayList<>();
        UserDataUtil userDataUtil = new UserDataUtil(getApplicationContext());
        MeterUtil meterUtil= new MeterUtil(getApplicationContext());
        MeterBoxUtil meterBoxUtil = new MeterBoxUtil(getApplicationContext());
        List<User> userList = userDataUtil.queryUserByAreaId(stationArea.getId());
        for(int i = 0;i<userList.size();i++){
            List<String> datas = new ArrayList<>();
            User user = userList.get(i);
            datas.add(String.valueOf(i+1));
            datas.add(user.getBarCode());
            datas.add(user.getUserCode());
            datas.add(user.getUsername());
            datas.add(user.getAddress());
            datas.add("1");
            datas.add("");
            Meter meter = meterUtil.queryMeterByMeterCode(user.getBarCode());
            if(meter!=null){
                MeterBox meterBox = meterBoxUtil.listOneMeterBox(meter.getBoxId());
                datas.add(meterBox.getBoxCode());
            }else{
                datas.add("");
            }
            dataList.add(datas);
        }

        ExportEquipment exportEquipment = new ExportEquipment();
        exportEquipment.setFileName(stationArea.getName());
        exportEquipment.setData(dataList);
        exportEquipment.setFolderName(stationArea.getName());
        exportEquipment.setTitleName(titles);
        exportExcel(getApplicationContext(),exportEquipment);
    }

    private void exportDyDX(StationArea stationArea){
        // 尝试导出设备线路清单到Excel文件
        try{
            // 定义Excel表头
            String[] titles = {"序号","导线名称","长度(m)","埋深(cm)"};
            // 数据列表，用于存储要导出的数据
            List<List<String>> dataList = new ArrayList<>();
            // 实例化设备线路工具类和设备工具类
            DyEquipmentLineUtil dyEquipmentLineUtil = new DyEquipmentLineUtil(getApplicationContext());
            DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
            // 根据区域ID查询设备线路信息
            List<DyEquipmentLine> dyEquipmentLines = dyEquipmentLineUtil.queryEquipmentLineByLineIdNoBox(stationArea.getId());
            int j = 1; // 用于为序号列编号

            // 遍历查询到的设备线路信息
            for(int i = 0;i<dyEquipmentLines.size();i++){
                DyEquipmentLine dyEquipmentLine = dyEquipmentLines.get(i);
                // 获取线路起始和结束的设备信息
                DyEquipment start = dyEquipmentUtil.listOneDyEquipment(dyEquipmentLine.getStartEquipmentId());
                DyEquipment end = dyEquipmentUtil.listOneDyEquipment(dyEquipmentLine.getEndEquipmentId());
                // 当起始和结束设备信息都不为空时，处理并添加到数据列表
                if(start!=null && end!=null){
                    List<String> datas = new ArrayList<>();
                    datas.add(String.valueOf(j)); // 序号
                    datas.add(start.getName()+"—"+end.getName()); // 导线名称
                    datas.add(getDX(dyEquipmentLine.getId())); // 长度
                    datas.add(dyEquipmentLine.getJscm().toString()); // 埋深
                    j++;
                    dataList.add(datas);
                }
            }
            // 配置导出信息并导出Excel文件
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"导线清单列表");
            exportEquipment.setData(dataList);
            exportEquipment.setFolderName(stationArea.getName());
            exportEquipment.setTitleName(titles);
            exportExcel(getApplicationContext(),exportEquipment);

        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"导出低压导线失败",Toast.LENGTH_LONG).show();
        }
    }



    private void exportDyDLD(StationArea stationArea){
        try{
            String[] titles = {"序号","电缆名称","长度(m)"};
            List<List<String>> dataList = new ArrayList<>();
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"电缆段清单列表");
            exportEquipment.setData(dataList);
            exportEquipment.setFolderName(stationArea.getName());
            exportEquipment.setTitleName(titles);
            exportExcel(getApplicationContext(),exportEquipment);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"电缆段清单列表导出失败",Toast.LENGTH_LONG).show();
        }
    }


    private void exportDyGTQD(StationArea stationArea){
        try{
            String[] titles = {"序号","杆塔名称","长度(m)"};
            List<List<String>> dataList = new ArrayList<>();
            DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
            List<DyEquipment> dyEquipmentList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndDY2(stationArea.getId());
            for(int i = 0;i<dyEquipmentList.size();i++){
                DyEquipment dyEquipment = dyEquipmentList.get(i);
                Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(dyEquipment.getLat(),dyEquipment.getLon());
                List<String> datas = new ArrayList<>();
                datas.add(String.valueOf(i+1));
                datas.add(dyEquipment.getName());
                datas.add(getDJ(dyEquipment.getId()));
                dataList.add(datas);
            }
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"杆塔清单列表");
            exportEquipment.setData(dataList);
            exportEquipment.setFolderName(stationArea.getName());
            exportEquipment.setTitleName(titles);
            exportExcel(getApplicationContext(),exportEquipment);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"杆塔清单列表导出失败",Toast.LENGTH_LONG).show();
        }
    }


    private void exportJLXMB(StationArea stationArea){
        try{
            String[] titles = {"序号","业务系统ID","计量箱名称","计量箱条码编号","设备类型","表箱类型","安装地址","行","列","材质","管理单位","资产单位","资产性质","城乡类别","电压等级","经度（X坐标）","纬度（Y坐标）","高程","挂接设备类型","挂接设备名称","接入点标识"};
            List<List<String>> dataList = new ArrayList<>();
            MeterBoxUtil meterBoxUtil = new MeterBoxUtil(getApplicationContext());
            DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
            List<MeterBox> boxList = meterBoxUtil.queryMeterBoxByLineIdTime2(stationArea.getId());
            for(int i = 0;i<boxList.size();i++){
                MeterBox meterBox = boxList.get(i);
                DyEquipment dyEquipment = dyEquipmentUtil.listOneDyEquipment(meterBox.getPreviousEquipmentId());
//                Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(meterBox.getLat(),meterBox.getLon());
                List<String> datas = new ArrayList<>();
                datas.add(String.valueOf(i+1));
                datas.add("");
                datas.add(meterBox.getBoxCode());
                datas.add(meterBox.getBoxName()==null?"":meterBox.getBoxName());
                datas.add("计量箱");
                datas.add(meterBox.getType());
                datas.add(meterBox.getCellUnit());
                datas.add(String.valueOf(meterBox.getRow()));
                datas.add(String.valueOf(meterBox.getColumn()));
                datas.add(meterBox.getMaterial()==null?"":meterBox.getMaterial());
                datas.add("");
                datas.add("");
                datas.add("");
                datas.add("");
                datas.add("");
                datas.add(String.format("%.8f", dyEquipment.getLon()));
                datas.add(String.format("%.8f", dyEquipment.getLat()));
                datas.add("0.0");
                datas.add(dyEquipment.getType());
                datas.add(dyEquipment.getName()+"用户接入点");
                datas.add("1");
                dataList.add(datas);
            }
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"计量箱模板");
            exportEquipment.setData(dataList);
            exportEquipment.setFolderName(stationArea.getName());
            exportEquipment.setTitleName(titles);
            exportExcel(getApplicationContext(),exportEquipment);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"导出计量箱模板失败",Toast.LENGTH_LONG).show();
        }
    }

    private void exportEquipmentList(StationArea stationArea){
        try{
            DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
            List<DyEquipment> dyEquipmentList = dyEquipmentUtil.queryDyEquipmentByAreaIdAscTime(stationArea.getId());
            String[] titles = {"序号","设备名称","前端设备","经度","纬度","高程","设备材料/厂家","同杆架设","连接线类型","连接线型号","设备类型","缺陷信息","连接方式"};
            List<List<String>> dataList = new ArrayList<>();
            int num = 0;
            for(int i = 0;i<dyEquipmentList.size();i++){
                List<String> itemData = new ArrayList<>();
                DyEquipment dyEquipment = dyEquipmentList.get(i);

                //导出照片
                copyPhoto4e(dyEquipment,stationArea.getName());
//                Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(dyEquipment.getLat(),dyEquipment.getLon());
                num = num+1;
                itemData.add(String.valueOf(num));
                itemData.add(dyEquipment.getName());
                DyEquipment exq = dyEquipmentUtil.listOneDyEquipment(dyEquipment.getPreviousEquipmentId());

                if(exq!=null){
                    if(exq.getType().equals("配电箱")){
                        SwitchEquipment switchEquipment = switchEquipmentUtil.querySwitchEquipmentByEid(dyEquipment.getId()).get(0);
                        Switch s = switchUtil.listOneSwitch(switchEquipment.getSid());
                        if(s!=null){
                            itemData.add(s.getName());
                        }else{
                            itemData.add("");
                        }
                    }else{
                        itemData.add(exq.getName());
                    }
                }else{
                    itemData.add("");
                }

                itemData.add(String.format("%.8f", dyEquipment.getLon()));
                itemData.add(String.format("%.8f", dyEquipment.getLat()));
                itemData.add("0.0");
                itemData.add(dyEquipment.getGtxh()==null?"":dyEquipment.getGtxh());
                itemData.add(dyEquipment.getTgjs()==null?"":dyEquipment.getTgjs());
                itemData.add(dyEquipment.getDxlx()==null?"":dyEquipment.getDxlx());
                itemData.add(dyEquipment.getDlxh()==null?"":dyEquipment.getDlxh());
                itemData.add(ZDAPP.EquipmentTypeName2.get(dyEquipment.getType()));
                itemData.add(dyEquipment.getQxxx()==null?"":dyEquipment.getQxxx());
                itemData.add(dyEquipment.getLxfs()==null?"":dyEquipment.getLxfs());
                dataList.add(itemData);
                if(dyEquipment.getType().equals("配电箱")){
                    List<Switch> switchList = switchUtil.querySwitchByeId(dyEquipment.getId());
                    for(int j = 0;j<switchList.size();j++){
                        Switch sItem = switchList.get(j);
                        List<String> itemData1 = new ArrayList<>();
                        num = num+1;
                        itemData1.add(String.valueOf(num));
                        itemData1.add(sItem.getName());
                        itemData1.add(dyEquipment.getName());
                        itemData1.add(String.format("%.8f", dyEquipment.getLon()));
                        itemData1.add(String.format("%.8f", dyEquipment.getLat()));
                        itemData1.add("0.0");
                        itemData1.add("");
                        itemData1.add("");
                        itemData1.add("");
                        itemData1.add("");
                        itemData1.add("kg");
                        itemData1.add("");
                        itemData1.add("");
                        dataList.add(itemData1);
                    }
                }
            }
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"杆塔信息");
            exportEquipment.setData(dataList);
            exportEquipment.setFolderName(stationArea.getName());
            exportEquipment.setTitleName(titles);
            exportExcelgt(getApplicationContext(),exportEquipment);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"导出杆塔信息错误",Toast.LENGTH_LONG).show();
        }
    }


    private void copyPhoto4e(DyEquipment dyEquipment,String sname){
        String nPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/dyEquipment/"+dyEquipment.getId()+"/";

        String ePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/"+sname+"/照片/"+dyEquipment.getType()+"/"+dyEquipment.getName();
        String[] phototype = {"qm","mp","qx","tt","qzj"};
        String[] photoName = {"全貌","铭牌","缺陷","塔头","墙支架"};
        for(int i = 0;i<phototype.length;i++){
            List<String> files = Utils.getFilesAllName(nPath+phototype[i]+"/");
            for(int j=0;j<files.size();j++){
                Utils.copyFile(files.get(j),ePath+"/"+dyEquipment.getName()+"-"+photoName[i]+(j+1)+".jpg");
            }
        }
    }

    private void copyPhoto4m(MeterBox meterBox,String sname){
        String nPath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/MeterBox/"+meterBox.getId()+"/";

        String ePath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/"+sname+"/照片/表箱/"+meterBox.getBoxCode();
        String[] phototype = {"dk","gb","qt"};
        String[] photoName = {"打开","关闭","其他"};
        for(int i = 0;i<phototype.length;i++){
            List<String> files = Utils.getFilesAllName(nPath+phototype[i]+"/");
            for(int j=0;j<files.size();j++){
                Utils.copyFile(files.get(j),ePath+"/"+meterBox.getBoxCode()+"-"+photoName[i]+(j+1)+".jpg");
            }
        }
    }


    private void exportMeterList(StationArea stationArea){
        try{
            MeterBoxUtil meterBoxUtil = new MeterBoxUtil(getApplicationContext());
            MeterUtil meterUtil = new MeterUtil(getApplicationContext());
            UserDataUtil userDataUtil = new UserDataUtil(getApplicationContext());
            List<MeterBox> meterBoxList = meterBoxUtil.queryMeterBoxByLineId(stationArea.getId());

            String[] titles = {"序号","客户编号","客户名称","计量箱条码(系统)","用电地址","表计编号","计量箱ID","计量箱条码","第几行","第几列","前端设备","计量箱总行","计量箱总列","小区单元号(站变)","表箱类型","表箱材质","系统台区","经度","纬度","高程","缺陷信息"};
            List<List<String>> dataList = new ArrayList<>();
            int n = 1;
            List<String> usercode = new ArrayList<>();
            for(int i = 0;i<meterBoxList.size();i++) {
                MeterBox meterBox = meterBoxList.get(i);
                //导出照片
                copyPhoto4m(meterBox,stationArea.getName());

//                Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(meterBox.getLat(),meterBox.getLon());
                List<Meter> meterList = meterUtil.queryMetersByBoxId(meterBox.getId());

                for(int j = 0;j<meterList.size();j++){
                    Meter meter = meterList.get(j);
                    List<String> itemData = new ArrayList<>();
                    itemData.add(String.valueOf(n));
                    User user = userDataUtil.queryUserByAreaIdAndUserCode(stationArea.getId(),meter.getMeterCode());
                    if(user!=null){
                        usercode.add(user.getUserCode());
                        itemData.add(user.getUserCode());
                        itemData.add(user.getUsername());
                        itemData.add(user.getBarCode());
                        itemData.add(user.getAddress());
                    }
                    else{
                        itemData.add("");
                        itemData.add("");
                        itemData.add("");
                        itemData.add("");
                    }
                    itemData.add(meter.getMeterCode());
                    itemData.add(meterBox.getBoxName());
                    itemData.add(meterBox.getBoxCode());
                    itemData.add(String.valueOf(meter.getRowNum()));
                    itemData.add(String.valueOf(meter.getColumnNum()));
                    DyEquipment dyEquipment = new DyEquipmentUtil(getApplicationContext()).listOneDyEquipment(meterBox.getPreviousEquipmentId());
                    itemData.add(dyEquipment.getName());
                    itemData.add(String.valueOf(meterBox.getRow()));
                    itemData.add(String.valueOf(meterBox.getColumn()));
                    itemData.add(meterBox.getCellUnit());
                    itemData.add(meterBox.getType());
                    itemData.add(meterBox.getMaterial());
                    itemData.add("");
                    itemData.add(String.format("%.8f", meterBox.getLon()));
                    itemData.add(String.format("%.8f", meterBox.getLat()));
                    itemData.add("0.0");
                    itemData.add(meterBox.getQxxx());
                    dataList.add(itemData);
                    n+=1;
                }
            }

            List<User> userList = userDataUtil.queryUserByAreaId(stationArea.getId());
            for(User user:userList){
                if(!usercode.contains(user.getUserCode())){
                    List<String> itemData = new ArrayList<>();
                    itemData.add(String.valueOf(n));
                    itemData.add(user.getUserCode());
                    itemData.add(user.getUsername());
                    itemData.add(user.getBarCode());
                    itemData.add(user.getAddress());
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    itemData.add("");
                    dataList.add(itemData);
                    n+=1;
                }
            }
            ExportEquipment exportEquipment = new ExportEquipment();
            exportEquipment.setFileName(stationArea.getName()+"用户信息");
            exportEquipment.setData(dataList);
            exportEquipment.setFolderName(stationArea.getName());
            exportEquipment.setTitleName(titles);
            exportExcel(getApplicationContext(),exportEquipment);
        }catch (Exception e){
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"导出用户信息发生错误",Toast.LENGTH_LONG).show();
        }

    }

    private void puthistory(){
        new PopWindow.Builder(DyWorkActivity.this)
                .setStyle(PopWindow.PopWindowStyle.PopAlert)
                .setMessage("导入历史数据将会备份当前数据")
                .setIsShowCircleBackground(false)
                .addItemAction(new PopItemAction("确定", new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        openFileH();
                    }
                }))
                .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Cancel))
                .setPopWindowMarginsF(DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),200), DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),0))
                .show();
    }


    private void openFile(){
        FilePicker.from(this)
                .chooseForBrowser()
                .setMaxCount(3)
                .setFileTypes("xls","xlsx","zip")
                .requestCode(REQUEST_CODE_CHOOSE)
                .start();
    }

    private void openFileH(){
        FilePicker.from(this)
                .chooseForBrowser()
                .setMaxCount(3)
                .setFileTypes("txt","zip")
                .requestCode(REQUEST_CODE_CHOOSE)
                .start();
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == REQUEST_CODE_CHOOSE) {
            ArrayList<EssFile> essFileList = data.getParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION);
            boolean fileOk = true;
            String filename = "";
            if(updateType==0){
                for (EssFile file : essFileList) {
                    try {
                        insertUser(file);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                updateHistory();
                clearData();
                for (EssFile file : essFileList) {
                    initHistory(file);
                }
            }


        }else{
            if(photoType.equals("qm")) {
                qmPhotoView.onActivityResultListener(requestCode, resultCode, data);
            }
            if(photoType.equals("mp")) {
                mpPhotoView.onActivityResultListener(requestCode, resultCode, data);
            }
        }

    }



    private void insertUser(EssFile file) throws IOException {
        File f = file.getFile();
        if(f.getName().indexOf("rar")>-1 ||f.getName().indexOf("zip")>-1){
            String autopath = Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/NZDAPP/"+UuidUtils.getStringID();
            new UpdateTask().execute(f.getPath(),autopath);
        }else{
            readExcelxlsx(f);
            initData();
        }

    }



    private String getFileContent(File file) {
        String content = "";
        if (!file.isDirectory()) {  //检查此路径名的文件是否是一个目录(文件夹)
            if (file.getName().endsWith("txt")) {//文件格式为""文件
                try {
                    InputStream instream = new FileInputStream(file);
                    if (instream != null) {
                        InputStreamReader inputreader
                                = new InputStreamReader(instream, "UTF-8");
                        BufferedReader buffreader = new BufferedReader(inputreader);
                        String line = "";
                        //分行读取
                        while ((line = buffreader.readLine()) != null) {
                            content += line + "\n";
                        }
                        instream.close();//关闭输入流
                    }
                } catch (java.io.FileNotFoundException e) {
                    Log.d("TestFile", "The File doesn't not exist.");
                } catch (IOException e) {
                    Log.d("TestFile", e.getMessage());
                }
            }
        }
        return content;
    }

    public void readExcelxlsx(File file) {
        try {
            /**
             * 后续考虑问题,比如Excel里面的图片以及其他数据类型的读取
             **/
            Workbook book = Workbook
                    .getWorkbook(file);
            book.getNumberOfSheets();
            // 获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();
            String title = sheet.getCell(0,0).getContents();
            String areaId = "";

            if(stationAreaUtil.queryStationAreaByAreaName(title)==null){
                Calendar calendar = Calendar.getInstance ();
                StationArea stationArea = new StationArea();
                areaId = UuidUtils.getStringID();
                stationArea.setId(areaId);
                stationArea.setName(title);
                stationArea.setType(type);
                stationArea.setStatue("未采集");
                stationArea.setUpdateType("update");
                stationArea.setCreateTime(calendar.get(calendar.YEAR)+"年"+(calendar.get(calendar.MONTH)+1)+"月"+calendar.get(calendar.DATE)+"日");
                stationAreaUtil.insertStationArea(stationArea);
            }else {
                StationArea stationArea = stationAreaUtil.queryStationAreaByAreaName(title);
                areaId = stationArea.getId();
            }
            UserDataUtil userDataUtil = new UserDataUtil(getApplicationContext());
            for (int i = 2; i < Rows; i++) {
                if(!sheet.getCell(2,i).getContents().equals("")){
                    if(userDataUtil.queryUserByAreaIdAndUserCode(areaId,sheet.getCell(4,i).getContents())==null){
                        User user = new User();
                        user.setId(UuidUtils.getStringID());
                        user.setAddress(sheet.getCell(3,i).getContents());
                        user.setUserCode(sheet.getCell(1,i).getContents());
                        user.setUsername(sheet.getCell(2,i).getContents());
                        user.setElectricityMeterOfficeNumber(sheet.getCell(0,i).getContents());
                        user.setBarCode(sheet.getCell(4,i).getContents());
                        user.setUserBoxNumber(sheet.getCell(5,i).getContents());
                        user.setModifyBoxCode(sheet.getCell(6,i).getContents());
                        user.setSerialNumber(sheet.getCell(7,i).getContents());
                        user.setAreaId(areaId);
                        userDataUtil.insertUser(user);
                    }
                }
            }
            book.close();
        } catch (Exception e) {
            e.printStackTrace();
            Toasty.error(getApplicationContext(),   "导入失败，请检查导入数据是否正确", Toast.LENGTH_LONG, true).show();
        }
    }

    //导出
    private void exportExcel(Context context, ExportEquipment exportEquipment) {
        String autopath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/"+exportEquipment.getFolderName();
        File file = new File(autopath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String excelFileName = "/"+exportEquipment.getFileName()+".xls";
        String[] title = exportEquipment.getTitleName();
        autopath = autopath + excelFileName;//文件的路径
        ExcelUtils.initExcel4dy(autopath, exportEquipment.getFileName(),title);
        ExcelUtils.writeObjListToExcel4dy(exportEquipment.getData(), autopath, context);
    }

    //导出
    private void exportExcelgt(Context context,ExportEquipment exportEquipment) {
        String autopath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/"+exportEquipment.getFolderName();
        File file = new File(autopath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String excelFileName = "/"+exportEquipment.getFileName()+".xls";
        String[] title = exportEquipment.getTitleName();
        autopath = autopath + excelFileName;//文件的路径
        ExcelUtils.initExcel4gt(autopath, exportEquipment.getFileName(),title);
        ExcelUtils.writeObjListToExcel4gt(exportEquipment.getData(), autopath, context);
    }

    //导出模板
    /**
     * 导出设备信息到Excel文件。
     *
     * @param context Android上下文对象，用于访问文件系统。
     * @param exportEquipment 包含导出配置信息的对象，如文件夹名和文件名。
     * @param stationArea 设备所在区域的信息，用于生成导出的Excel数据。
     *
     * 此方法首先构建导出文件的路径，并检查文件夹是否存在，如果不存在则创建。
     * 然后初始化Excel模版，根据不同的数据类型，将处理后的数据写入到相应的Excel工作表中。
     */
    private void exportExcelModel(Context context,ExportEquipment exportEquipment,StationArea stationArea) {
        // 构建导出文件的绝对路径
        String autopath = Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/NZDAPP/"+exportEquipment.getFolderName();
        File file = new File(autopath);
        // 如果文件夹不存在，则创建文件夹
        if (!file.exists()) {
            file.mkdirs();
        }
        // 拼接Excel文件名
        String excelFileName = "/"+exportEquipment.getFileName()+".xls";
        autopath = autopath + excelFileName;//文件的路径
        // 初始化Excel模版
        ExcelUtils.initExcelModel(autopath, exportEquipment.getFileName());
        // 初始化数据模型
        HashMap<String,String> hashMap = initModelData(stationArea);
        // 分别将不同类型的设备数据写入到Excel的不同工作表中
        ExcelUtils.writeObjListToExcel4Model(modeldataDygt(stationArea,hashMap),autopath,0);
        ExcelUtils.writeObjListToExcel4Model(modeldataDydx(stationArea,hashMap),autopath,1);
        ExcelUtils.writeObjListToExcel4Model(modeldataQzj(stationArea,hashMap),autopath,2);
        ExcelUtils.writeObjListToExcel4Model(modeldataJLX(stationArea,hashMap),autopath,3);
        ExcelUtils.writeObjListToExcel4Model(modeldataJHX(stationArea,hashMap),autopath,4);
        ExcelUtils.writeObjListToExcel4Model(modeldataJHX(stationArea,hashMap),autopath,4);
    }

    private HashMap<String,String> initModelData(StationArea stationArea){

        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        //杆塔
        List<DyEquipment> gtList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndDY2(stationArea.getId());
        HashMap<String,String> hashMap = new HashMap<>();
        for(int i = 0;i<gtList.size();i++){
            DyEquipment dyEquipment = gtList.get(i);
            hashMap.put(dyEquipment.getId(),"tower"+Utils.getName(i+1));
        }

        //墙支架
        List<DyEquipment> qzjList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndQzj(stationArea.getId());
        for(int i = 0;i<qzjList.size();i++){
            DyEquipment dyEquipment = qzjList.get(i);
            hashMap.put(dyEquipment.getId(),"qzj"+Utils.getName(i+1));
        }
        //分支箱
        List<DyEquipment> fzxList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndfzx(stationArea.getId());
        for(int i = 0;i<fzxList.size();i++){
            DyEquipment dyEquipment = fzxList.get(i);
            hashMap.put(dyEquipment.getId(),"fzx"+Utils.getName(i+1));
        }
        //拐点
        List<DyEquipment> gdList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndGD(stationArea.getId());
        for(int i = 0;i<gdList.size();i++){
            DyEquipment dyEquipment = gdList.get(i);
            hashMap.put(dyEquipment.getId(),"gd"+Utils.getName(i+1));
        }

        //拐点
        List<DyEquipment> dbList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndDB(stationArea.getId());
        for(int i = 0;i<dbList.size();i++){
            DyEquipment dyEquipment = dbList.get(i);
            if(dyEquipment.getType().equals("配电箱")){
                hashMap.put(dyEquipment.getId(),"pdx"+Utils.getName(i+1));
            }else{
                hashMap.put(dyEquipment.getId(),"byq"+Utils.getName(i+1));
            }
        }


        MeterBoxUtil meterBoxUtil = new MeterBoxUtil(getApplicationContext());
        List<MeterBox> boxList = meterBoxUtil.queryMeterBoxByLineIdTime2(stationArea.getId());
        for(int i = 0;i<boxList.size();i++){
            MeterBox meterBox = boxList.get(i);
            hashMap.put(meterBox.getId(),"JLX"+Utils.getName(i+1));

        }
        return hashMap;
    }

    private String getDJ(String id){
        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        DyEquipment dyEquipment = dyEquipmentUtil.listOneDyEquipment(id);
        DyEquipment exdyEquipment = dyEquipmentUtil.listOneDyEquipment(dyEquipment.getPreviousEquipmentId());
        if(exdyEquipment.getType().equals("低压")){
//            LatLng stlatLng = new LatLng(dyEquipment.getLat(),dyEquipment.getLon());
//            LatLng edlatLng = new LatLng(exdyEquipment.getLat(),exdyEquipment.getLon());
//            double dis = AMapUtils.calculateLineDistance(stlatLng,edlatLng);
            PolylineBuilder lineGeometry = new PolylineBuilder(SpatialReferences.getWgs84());
            lineGeometry.addPoint(new Point(dyEquipment.getLon(),dyEquipment.getLat()));
            lineGeometry.addPoint(new Point(exdyEquipment.getLon(),exdyEquipment.getLat()));
            double dis = GeometryEngine.lengthGeodetic(lineGeometry.toGeometry(), new LinearUnit(LinearUnitId.METERS), GeodeticCurveType.SHAPE_PRESERVING);

            return String.format("%.2f", dis);
        }
        return "";
    }

    private String getDX(String id){
        DyEquipmentLineUtil dyEquipmentLineUtil = new DyEquipmentLineUtil(getApplicationContext());
        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        DyEquipmentLine dyEquipmentLine = dyEquipmentLineUtil.listOneDyEquipmentLine(id);
        DyEquipment start = dyEquipmentUtil.listOneDyEquipment(dyEquipmentLine.getStartEquipmentId());
        DyEquipment end = dyEquipmentUtil.listOneDyEquipment(dyEquipmentLine.getEndEquipmentId());

//        LatLng stlatLng = new LatLng(start.getLat(),start.getLon());
//        LatLng edlatLng = new LatLng(end.getLat(),end.getLon());
//        double dis = AMapUtils.calculateLineDistance(stlatLng,edlatLng);
        PolylineBuilder lineGeometry = new PolylineBuilder(SpatialReferences.getWgs84());
        lineGeometry.addPoint(new Point(start.getLon(),start.getLat()));
        lineGeometry.addPoint(new Point(end.getLon(),end.getLat()));
        double dis = GeometryEngine.lengthGeodetic(lineGeometry.toGeometry(), new LinearUnit(LinearUnitId.METERS), GeodeticCurveType.SHAPE_PRESERVING);


//        double dis = 0.0;
        return String.format("%.2f", dis);

    }

    private  List<List<String>> modeldataDydx(StationArea stationArea,HashMap<String,String> hashMap){
        List<List<String>> dataList = new ArrayList<>();
        DyEquipmentLineUtil dyEquipmentLineUtil = new DyEquipmentLineUtil(getApplicationContext());
        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        List<DyEquipmentLine> dyEquipmentLines = dyEquipmentLineUtil.queryEquipmentLineByLineIdNoBox(stationArea.getId());
        int j = 1;
        for(int i = 0;i<dyEquipmentLines.size();i++){
            DyEquipmentLine dyEquipmentLine = dyEquipmentLines.get(i);
            DyEquipment start = dyEquipmentUtil.listOneDyEquipment(dyEquipmentLine.getStartEquipmentId());
            DyEquipment end = dyEquipmentUtil.listOneDyEquipment(dyEquipmentLine.getEndEquipmentId());
            if(start!=null && end!=null){
                List<String> datas = new ArrayList<>();
                datas.add(String.valueOf(j));
                datas.add("");
                datas.add(start.getName()+"-"+end.getName());
                datas.add("");
                datas.add(ZDAPP.EquipmentTypeName2.get(start.getType()));
                datas.add(hashMap.get(start.getId()));
                datas.add(hashMap.get(start.getId())+","+hashMap.get(end.getId()));
                datas.add(ZDAPP.EquipmentTypeName2.get(end.getType()));
                datas.add(hashMap.get(end.getId()));
                datas.add("");
                datas.add("");
                datas.add("");
                datas.add("");
                datas.add(start.getDlxh()==null?"":start.getDlxh());
                datas.add("");
                datas.add("");
                datas.add(getDX(dyEquipmentLine.getId()));
                datas.add("");
                datas.add("");
                datas.add("");
                datas.add("");
                j++;
                dataList.add(datas);
            }

        }
        return dataList;
    }

    private List<List<String>> modeldataDygt(StationArea stationArea,HashMap<String,String> hashMap){
        List<List<String>> dataList = new ArrayList<>();
        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        List<DyEquipment> dyEquipmentList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndDY2(stationArea.getId());
        for(int i = 0;i<dyEquipmentList.size();i++){
            DyEquipment dyEquipment = dyEquipmentList.get(i);
//            Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(dyEquipment.getLat(),dyEquipment.getLon());
            List<String> datas = new ArrayList<>();
            datas.add(String.valueOf(i+1));
            datas.add("");
            datas.add(dyEquipment.getName());
            datas.add("");
            datas.add(hashMap.get(dyEquipment.getId()));
            datas.add(dyEquipment.getGtxz()==null?"":dyEquipment.getGtxz());
            datas.add(String.format("%.8f", dyEquipment.getLon()));
            datas.add(String.format("%.8f", dyEquipment.getLat()));
            if(dyEquipment.getTgjs()==null || dyEquipment.getTgjs().equals("无同杆架设")){
                datas.add("0");
            }else{
                datas.add(dyEquipment.getTgjs());
            }
            datas.add(dyEquipment.getGtcz()==null?"":dyEquipment.getGtcz());
            datas.add("");
            datas.add("");
            if(dyEquipment.getTgjs()==null || dyEquipment.getTgjs().equals("无同杆架设")){
                datas.add("无并架");
            }else{
                datas.add("同压并架");
            }
            datas.add("否");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add(getDJ(dyEquipment.getId()));
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add(dyEquipment.getGtxh()==null?"":dyEquipment.getGtxh());
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            if(dyEquipment.getTgjs()==null || dyEquipment.getTgjs().equals("无同杆架设")){
                datas.add("否");
            }else{
                datas.add("是");
            }
            dataList.add(datas);
        }
        return dataList;
    }

    private List<List<String>> modeldataQzj(StationArea stationArea,HashMap<String,String> hashMap){
        List<List<String>> dataList = new ArrayList<>();
        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        List<DyEquipment> qzjList = dyEquipmentUtil.queryDyEquipmentByAreaIdAndQzj(stationArea.getId());
        for(int i = 0;i<qzjList.size();i++){
            DyEquipment dyEquipment = qzjList.get(i);
//            Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(dyEquipment.getLat(),dyEquipment.getLon());
            List<String> datas = new ArrayList<>();
            datas.add(String.valueOf(i+1));
            datas.add("");
            datas.add(dyEquipment.getName());
            datas.add(hashMap.get(dyEquipment.getId()));
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add(String.format("%.8f", dyEquipment.getLon()));
            datas.add(String.format("%.8f", dyEquipment.getLat()));
            datas.add(dyEquipment.getSbzt()==null?"":dyEquipment.getSbzt());
            dataList.add(datas);
        }
        return dataList;
    }

    private List<List<String>> modeldataJLX(StationArea stationArea,HashMap<String,String> hashMap){
        List<List<String>> dataList = new ArrayList<>();
        MeterBoxUtil meterBoxUtil = new MeterBoxUtil(getApplicationContext());
        List<MeterBox> boxList = meterBoxUtil.queryMeterBoxByLineIdTime2(stationArea.getId());
        for(int i = 0;i<boxList.size();i++){
            MeterBox meterBox = boxList.get(i);
//            Map<String, Double> map =  LatlonUtil.transformGCJ2WGS(meterBox.getLat(),meterBox.getLon());
            List<String> datas = new ArrayList<>();
            datas.add(String.valueOf(i+1));
            datas.add("");
            datas.add(meterBox.getBoxCode()+"接入点");
            datas.add("");
            datas.add(hashMap.get(meterBox.getId()));
            datas.add(String.format("%.8f", meterBox.getLon()));
            datas.add(String.format("%.8f", meterBox.getLat()));
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add(meterBox.getSbzt()==null?"":meterBox.getSbzt());
            dataList.add(datas);
        }
        return dataList;
    }

    private List<List<String>> modeldataJHX(StationArea stationArea,HashMap<String,String> hashMap){
        List<List<String>> dataList = new ArrayList<>();
        MeterBoxUtil meterBoxUtil = new MeterBoxUtil(getApplicationContext());
        DyEquipmentUtil dyEquipmentUtil = new DyEquipmentUtil(getApplicationContext());
        List<MeterBox> boxList = meterBoxUtil.queryMeterBoxByLineIdTime2(stationArea.getId());
        for(int i = 0;i<boxList.size();i++){
            DyEquipment dyEquipment = dyEquipmentUtil.listOneDyEquipment(boxList.get(i).getPreviousEquipmentId());
            List<String> datas = new ArrayList<>();
            datas.add(String.valueOf(i+1));
            datas.add("");
            datas.add(dyEquipment.getName()+"接户线");
            datas.add("");
            if(dyEquipment.getType().equals(ZDAPP.EQUIPMENT_DY)){
                datas.add("运行杆塔");
            }else{
                datas.add(dyEquipment.getType());
            }
            datas.add(hashMap.get(dyEquipment.getId()));
            datas.add("用户接入点");
            datas.add(hashMap.get(boxList.get(i).getId()));
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add(dyEquipment.getSbzt()==null?"":dyEquipment.getSbzt());
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("");
            datas.add("导线");
            dataList.add(datas);
        }
        return dataList;
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    private int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }



    private void editArea(StationArea stationArea){
        View areaView = View.inflate(getApplicationContext(), R.layout.area_detail_layout, null);
        FormEditView areaNameView = areaView.findViewById(R.id.area_name);
        areaNameView.getContent().setText(stationArea.getName());
        FormEditView areaTypeView = areaView.findViewById(R.id.area_type);
        areaTypeView.getContent().setText(stationArea.getType());
        TextView saveAreaView = areaView.findViewById(R.id.save_area);
        saveAreaView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(areaNameView.getValue().equals("")){
                    Toasty.error(getApplicationContext(),   "台区名称不能为空", Toast.LENGTH_LONG, true).show();
                }
                else if(stationAreaUtil.queryStationAreaByAreaName(areaNameView.getValue())!=null && !stationAreaUtil.queryStationAreaByAreaName(areaNameView.getValue()).getId().equals(stationArea.getId())){
                    Toasty.error(getApplicationContext(),   "该台区已存在", Toast.LENGTH_LONG, true).show();
                }else{
                    stationArea.setName(areaNameView.getValue());
                    stationArea.setType(areaTypeView.getValue());
                    stationAreaUtil.updateStationArea(stationArea);
                    Toasty.success(getApplicationContext(),   "保存台区成功", Toast.LENGTH_LONG, true).show();
                }
            }
        });
        areaTypeView.getContent().setFocusable(false);
        mpPhotoView = areaView.findViewById(R.id.area_mp_photo);
        qmPhotoView = areaView.findViewById(R.id.area_qm_photo);
        List<DyEquipment> dyEquipmentList = new DyEquipmentUtil(getApplicationContext()).queryDyEquipmentByAreaIdAndByq2(stationArea.getId());
        if(dyEquipmentList.size()==0){
            mpPhotoView.setVisibility(View.GONE);
            qmPhotoView.setVisibility(View.GONE);
        }
        else{
            mpPhotoView.initData("dyEquipment",dyEquipmentList.get(0).getId(),"mp");
            mpPhotoView.setOnclickPhotoListener(this);
            mpPhotoView.setmActivity(DyWorkActivity.this);
            qmPhotoView.initData("dyEquipment",dyEquipmentList.get(0).getId(),"qm");
            qmPhotoView.setOnclickPhotoListener(this);
            qmPhotoView.setmActivity(DyWorkActivity.this);
        }


        popupWindow3 = new PopWindow.Builder(DyWorkActivity.this)
                .setStyle(PopWindow.PopWindowStyle.PopAlert)
                .setMessage("编辑台区内容")
                .addContentView(areaView)
                .setIsShowCircleBackground(false)
                .addItemAction(new PopItemAction("进入台区", PopItemAction.PopItemStyle.Normal, new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        popupWindow3.dismiss();
                        openWork(stationArea);
                    }
                }))
                .setPopWindowMarginsF(DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),200), DeviceUtils.dip2px4View(getApplicationContext(),30), DeviceUtils.dip2px4View(getApplicationContext(),0))
                .show();
    }

    @Override
    public void onClickPhotoListener(String type) {
        photoType = type;
    }

    @Override
    public void openPhotoView(String path) {
        Intent intent = new Intent(DyWorkActivity.this,PhotoDetailActivity.class);
        intent.putExtra("filePath",path);
        startActivity(intent);
    }

    class UpdateTask extends AsyncTask<String, Integer, Boolean> {
        String filePath = "";

        @Override
        protected void onPreExecute() {


        }

        @Override
        protected Boolean doInBackground(String... params) {
            filePath = params[1];
            try {
                Utils.upZipFile(new File(params[0]),params[1]);
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        @Override
        protected void onPostExecute(Boolean result) {
            if(result){
                List<String> filelist = Utils.getFilesAllName4xls(filePath+"/");
                for(String s:filelist){
                    readExcelxlsx(new File(s));
                }
                Utils.deleteDirWihtFile(new File(filePath));
            }
        }
    }


    class InitdateTask extends AsyncTask<String, Integer, Boolean> {
        String filePath = "";

        @Override
        protected void onPreExecute() {

//            Toast.makeText(getApplicationContext(),"正在加载数据……",Toast.LENGTH_SHORT).show();
        }

        @Override
        protected Boolean doInBackground(String... params) {
            table.setTableData(null);
            dyWorkInfos = new ArrayList<>();
            for (int i = 0; i < workLines.size(); i++) {
                StationArea stationArea = workLines.get(i);
                DyWorkInfo dyWorkInfo = new DyWorkInfo();
                dyWorkInfo.setNumber(String.valueOf(i+1));
                dyWorkInfo.setName(stationArea.getName());
                dyWorkInfo.setType(stationArea.getType());
                dyWorkInfo.setBx(String.valueOf(meterBoxUtil.queryMeterBoxByLineId(stationArea.getId()).size()));
                List<MeterBox> meterBoxList = meterBoxUtil.queryMeterBoxByLineId(stationArea.getId());
                int meterNumber = 0;
                for(MeterBox meterBox:meterBoxList){
                    meterNumber+= meterUtil.queryMetersByBoxId(meterBox.getId()).size();
                }
                dyWorkInfo.setUser(String.valueOf(meterNumber));
                dyWorkInfo.setSystem(String.valueOf(userDataUtil.queryUserByAreaId(stationArea.getId()).size()));
                dyWorkInfo.setDate(stationArea.getCreateTime());
                dyWorkInfos.add(dyWorkInfo);
            }
            return true;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        @Override
        protected void onPostExecute(Boolean result) {
            if(result){
                if(dyWorkInfos.size()>0){
                    table.setVisibility(View.VISIBLE);
                    table.setData(dyWorkInfos);
                    table.getTableData().setOnRowClickListener(new TableData.OnRowClickListener<DyWorkInfo>() {

                        @Override
                        public void onClick(Column column, DyWorkInfo dyWorkInfo, int col, int row) {
                            new PopWindow.Builder(DyWorkActivity.this)
                                    .setStyle(PopWindow.PopWindowStyle.PopUp)
                                    .setTitle(workLines.get(row).getName()+"\n请选择你要的操作")
                                    .addItemAction(new PopItemAction("进入台区", PopItemAction.PopItemStyle.Normal, new PopItemAction.OnClickListener(){
                                        @Override
                                        public void onClick() {
                                            openWork(workLines.get(row));
                                        }
                                    }))
                                    .addItemAction(new PopItemAction("编辑", PopItemAction.PopItemStyle.Normal, new PopItemAction.OnClickListener() {
                                        @Override
                                        public void onClick() {
                                            editArea(workLines.get(row));
                                        }
                                    }))
                                    .addItemAction(new PopItemAction("删除", PopItemAction.PopItemStyle.Warning, new PopItemAction.OnClickListener() {
                                        @Override
                                        public void onClick() {
                                            delCom(workLines.get(row));
                                        }
                                    }))
                                    .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Cancel))
                                    .show();
                        }
                    });
                }else{
                    table.setVisibility(View.GONE);
                }
            }
        }
    }
}